package com.youzan.cloud.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 *
 * DateUtil
 *
 * 描述 ：日期工具类
 *
 * 2015年4月1日 下午3:35:37 test
 *
 * @version 1.0.0
 *
 */
@Slf4j
public class DateUtil {
	public final static String dateRegex = "(?:[0-9]{1,4}(?<!^0?0?0?0))-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1[0-9]|2[0-8]|(?:(?<=-(?:0?[13578]|1[02])-)(?:29|3[01]))|(?:(?<=-(?:0?[469]|11)-)(?:29|30))|(?:(?<=(?:(?:[0-9]{0,2}(?!0?0)(?:[02468]?(?<![13579])[048]|[13579][26]))|(?:(?:[02468]?[048]|[13579][26])00))-0?2-)(?:29)))";

	public final static String datetimeRegex = "(?:[0-9]{1,4}(?<!^0?0?0?0))-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1[0-9]|2[0-8]|(?:(?<=-(?:0?[13578]|1[02])-)(?:29|3[01]))|(?:(?<=-(?:0?[469]|11)-)(?:29|30))|(?:(?<=(?:(?:[0-9]{0,2}(?!0?0)(?:[02468]?(?<![13579])[048]|[13579][26]))|(?:(?:[02468]?[048]|[13579][26])00))-0?2-)(?:29)))";

	public static SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	/**
	 * 验证字符串是否为日期格式
	 * @param date
	 * @return 是否合格
	 */
	public static boolean dateVerify(String date){
		Pattern pattern = Pattern.compile(dateRegex);
		Matcher matcher = pattern.matcher(date);

		return matcher.matches();
	}
	/**
	 * 验证字符串是否为日期格式
	 * @param date
	 * @return 是否合格
	 */
	public static boolean datetimeVerify(String date){
		Pattern pattern = Pattern.compile(datetimeRegex);
		Matcher matcher = pattern.matcher(date);

		return matcher.matches();
	}
	/**
	 * 将时间字符串转化成日期
	 *
	 * @param dateStr
	 * @param format
	 * @return 将字符串变成日期
	 */
	public static Date stringToDate(String dateStr,String format){
		if("".equals(format) || format == null){
			format = "yyyy-MM-dd";
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);

		Date date = null;

		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			log.error("日期转换异常："+e.getMessage());
		}

		return date;
	}

	public static String dateToString(Date date,String format){
		if("".equals(format) || format == null){
			format = "yyyy-MM-dd";
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);

		String dateStr = sdf.format(date);

		return dateStr;
	}

	public static Date getCurrentDate(){

		return new Date();
	}
	/**
	 * @描述 获取时间段时间差
	 * @return int
	 */
	public static int CompareBetweenTime(String begTime,String endTime) {

		long jl;
		int days;
		try {
			jl = sdf.parse(begTime).getTime()-sdf.parse(endTime).getTime();
			days = Math.abs((int) Math.ceil((jl/(1000*60*60*24))))+1;//相隔天数
			return days;
		} catch (ParseException e) {
			log.error("日期转换异常："+e.getMessage());
		}
		return 0;
	}

	/**
	 * 两个时间相差距离多少天多少小时多少分多少秒
	 * @param str1 时间参数 1 格式：1990-01-01 12:00:00
	 * @param str2 时间参数 2 格式：2009-01-01 12:00:00
	 * @return String 返回值为：xx天xx小时xx分xx秒
	 */
	public static String GetDistanceTime(String str1, String str2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(str1);
			two = df.parse(str2);
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff ;
			if(time1<time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
		} catch (Exception e) {
			log.error("日期转换异常："+e.getMessage());
		}
		return day + "天" + hour + "小时" + min + "分" ;
	}

	/**
	 * 比较时间大小
	 * @param begTime 时间参数 1 格式：1990-01-01 12:00:00
	 * @param endTime 时间参数 2 格式：2009-01-01 12:00:00
	 * @return boolean 返回值为：开始时间是否大于结束时间，如果大于返回true，否则返回false
	 */
	public static boolean CompareTime(String begTime,String endTime) throws ParseException {
		Date begDate = sdf.parse(begTime);
		Date endDate = sdf.parse(endTime);
		try {
			if(begDate.after(endDate)) {
				return true;
			}else {
				return false;
			}

		} catch (Exception e) {
			log.error("日期转换异常："+e.getMessage());
		}
		return true;
	}

	/**
	 * 比较时间大小
	 * @param begTime 时间参数 1 格式：1990-01-01 12:00:00
	 * @param days  begTime向后增加多少天（注意是天而不是小时）
	 * @return boolean 返回值为：开始时间是否大于结束时间，如果大于返回true，否则返回false
	 */
	public static String GetAddTimeByDays(String begTime,Double days) throws ParseException {
		try {
			long times = sdf.parse(begTime).getTime();
			double addTimes = days*86400000;
			long mss = (long) (times + addTimes);
			Date date=new Date();
			date.setTime(mss);
			String resultTime = sdf.format(date);
			return resultTime;
		} catch (Exception e) {
			log.error("日期转换异常："+e.getMessage());
		}
		return null;
	}

	/**
	 * 比对开始时间和当前时间有没有超过时间间隔，超过了返回true
	 * @param compareDate 为输入比对时间
	 * @param num 为比对时间间隔
	 * @return
	 */

	public static boolean compareDate(Date compareDate,int num){
		try {
			long date0_num = compareDate.getTime()+num*60*1000;
			System.out.println("date0_num="+date0_num);
			long date1_num = System.currentTimeMillis();
			System.out.println("date1_num="+date1_num);
			int sum1 = (int)((date0_num-date1_num)/1000/60);
			System.out.println(sum1);
			if(date0_num>date1_num){
				return false;
			}
		} catch (Exception e) {   log.error("日期转换异常："+e.getMessage());  }
		return true;
	}

	/**
	 * @Description 是否为当天24h内
	 * @param date 要判断是否在当天24h内的时间
	 * @return
	 * boolean
	 */
	public static boolean isToday(String date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		boolean flag = false;
		//获取当前系统时间
		long longDate = System.currentTimeMillis();
		Date nowDate = new Date(longDate);
		String format = dateFormat.format(nowDate);
		String subDate = format.substring(0, 10);
		//定义每天的24h时间范围
		String beginTime = subDate + " 00:00:00";
		String endTime = subDate + " 23:59:59";
		Date paseBeginTime = null;
		Date paseEndTime = null;
		try {
			paseBeginTime = dateFormat.parse(beginTime);
			paseEndTime = dateFormat.parse(endTime);

		} catch (ParseException e) {
			log.error("日期转换异常："+e.getMessage());
		}
		Date inputJudgeDate;
		try {
			inputJudgeDate = dateFormat.parse(date);
			if(inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
				flag = true;
			}
		} catch (ParseException e1) {
			log.error("日期转换异常："+e1.getMessage());
		}
		return flag;
	}


}
